/*******************************************************************************
 * Copyright (c) 2010, 2030 www.mahao.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.fileupload.qiniu;




import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

/** 
 * ClassName: QiNiuDemo <br/> 
 * Function: 上传文件到七牛 <br/> 
 * date: 2017年4月20日 下午7:13:32 <br/> 
 * 
 * @author Mmmmm 
 * @version  1.0.0
 * @since JDK 1.8 
 */
public class QiNiuDemo {
	private static String AccessKey = "MboNV0VZMN_gNBzAJ5axLJhcWg_fcACZk7KtKK5V";
	private static String SecretKey = "VEKXKzXg9UT1nyl8_8J765cGylWLRLWR1YTkGveK";
	private static String bucketName = "test";
	//构造一个带指定Zone对象的配置类
	private static Configuration cfg = new Configuration(Zone.zone2());
	
	/**
	 * upload:(上传文件到七牛). <br/>
	 * @author Mmmmm 
	 * @param filePath 文件路径 格式是 D:\\qiniu\\test.png
	 * @param fileName  文件名称
	 * @since JDK 1.8
	 */
	public static void upload(String filePath,String fileName){
		//...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		//...生成上传凭证，然后准备上传
		String accessKey = AccessKey;
		String secretKey = SecretKey;
		String bucket = bucketName;
		//如果是Windows情况下，格式是 D:\\qiniu\\test.png
		String localFilePath = filePath;
		//默认不指定key的情况下，以文件内容的hash值作为文件名
		String key = fileName;
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		try {
		    Response response = uploadManager.put(localFilePath, key, upToken);
		    //解析上传成功的结果
		    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		    System.out.println(putRet.key);
		    System.out.println(putRet.hash);
		} catch (QiniuException ex) {
		    Response r = ex.response;
		    System.err.println(r.toString());
		    try {
		        System.err.println(r.bodyString());
		    } catch (QiniuException ex2) {
		        //ignore
		    }
		}
	}
	/**
	 * upload:(上传文件到七牛). <br/>
	 * @author Mmmmm 
	 * @param bt 文件的字符数组
	 * @param fileName 文件名称
	 * @since JDK 1.8
	 */
	public static void upload(byte[] bt,String fileName){
		//...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		//...生成上传凭证，然后准备上传
		String accessKey = AccessKey;
		String secretKey = SecretKey;
		String bucket = bucketName;
		byte[] localFilePath = bt;
		//默认不指定key的情况下，以文件内容的hash值作为文件名
		String key = fileName;
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		try {
		    Response response = uploadManager.put(localFilePath, key, upToken);
		    //解析上传成功的结果
		    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		    System.out.println(putRet.key);
		    System.out.println(putRet.hash);
		} catch (QiniuException ex) {
		    Response r = ex.response;
		    System.err.println(r.toString());
		    try {
		        System.err.println(r.bodyString());
		    } catch (QiniuException ex2) {
		        //ignore
		    }
		}
	}
	
}
